package com.leapp.sk.ui.activity.notify

import android.content.Intent
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import cc.vv.lkbasecomponent.http.LKHttp
import cc.vv.lkbasecomponent.http.lib.setting.Settings
import cc.vv.lklibrary.http.base.okhttpfactory.plugins.httpplugin.MediaTypeWrap
import com.example.baselibrary.util.LKToastUtil
import com.example.inter.RefreshLoadMoreInferface
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.example.util.ISTECHER
import com.example.util.finallist.SKCODE_200
import com.leapp.sk.R
import com.leapp.sk.app.CLASS_NOTIFY_PARENT
import com.leapp.sk.app.CLASS_NOTIFY_TEACHER
import com.leapp.sk.app.HELP_FEED_LIST
import com.leapp.sk.bean.ClassNotifyEntiry
import com.leapp.sk.bean.ParentClassNotifyEntity
import com.leapp.sk.bean.TeacherClassNotifyEntity
import com.leapp.sk.bean.request.classs.ParentClassNotifyRequestEntity
import com.leapp.sk.bean.request.notify.TeacherClassNotifyRequestEntity
import com.leapp.sk.bean.request.personal.FeedBackListRequestEntity
import com.leapp.sk.bean.request.personal.ParentClassNotifyResponseEntity
import com.leapp.sk.bean.response.notify.TeacherClassNotifyResponseEntity
import com.leapp.sk.bean.response.personal.FeedBackListResponseEntity
import com.leapp.sk.binder.BaseBinder
import com.leapp.sk.sever.notify.ClassNotifySever
import com.leapp.sk.sever.personal.FeedBackSever
import com.leapp.sk.viewdelegate.notify.ClassNotifyDelegate

/**
 * 班级通知
 * @author  chaohaipeng  on 2018/8/2.
 */
class ClassNotifyActivity :BaseActivityMVP<ClassNotifyDelegate>(), SwipeRefreshLayout.OnRefreshListener, RefreshLoadMoreInferface {
    override fun loadMoreData() {
        currentPage++
        if (ISTECHER){
            httTeacherList(false)
        }else{
            httParentList(false)
        }
    }

    /**
     * 当前页
     */
    var currentPage=1
    /**
     * 家长数据
     */
    val mutableParentListOf = mutableListOf<ParentClassNotifyEntity>()
    /**
     * 老师数据
     */
    val mutableTeachertListOf = mutableListOf<TeacherClassNotifyEntity>()
    override fun initDate() {
        if (ISTECHER){
            viewDelegate?.initTeacherAdapter(this,mutableTeachertListOf,this,this)
            httTeacherList(true)
        }else{
            viewDelegate?.initParentAdapter(this,mutableParentListOf,this,this)
            httParentList(true)
        }


    }

    override fun initView(p0: Bundle?) {
        viewDelegate.setTitle(resources.getString(R.string.string_notify_class_notify))
        if (ISTECHER){
            viewDelegate.initSendBtnVisible(true)
        }else{
            viewDelegate.initSendBtnVisible(false)
        }

    }

    override fun bindEvenListener() {
        viewDelegate.setOnClickListener(View.OnClickListener {
            val intent=Intent(this,SendNotifyActivity::class.java)
            startActivity(intent)
        },R.id.tv_asn_send_notify)



    }

    override fun getDelegateClass(): Class<ClassNotifyDelegate> {
        return ClassNotifyDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun onRefresh() {
        currentPage=1
        if (ISTECHER){
            httTeacherList(false)
        }else{
            httParentList(false)
        }
    }
    /**
     * 老师列表请求
     */
    private fun httTeacherList(isShowLoder:Boolean) {
        val teacherClassNotifyRequestEntity = TeacherClassNotifyRequestEntity()
        teacherClassNotifyRequestEntity.currPage = currentPage
        teacherClassNotifyRequestEntity.teacherId=teacherClassNotifyRequestEntity.userId
        LKHttp.post(CLASS_NOTIFY_TEACHER, teacherClassNotifyRequestEntity, TeacherClassNotifyResponseEntity::class.java, object : BaseCallBack<TeacherClassNotifyResponseEntity>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: TeacherClassNotifyResponseEntity?) {
                super.onSuccess(url, resultText, obj)
                viewDelegate?.stopSwipRefresh()
                if (obj?.code == SKCODE_200) {
                    if (currentPage == 1) {
                        mutableTeachertListOf.clear()
                    }
                    val teacherData = ClassNotifySever.getInstance().getTeacherData(obj, viewDelegate)
                    if (teacherData!==null){
                        mutableTeachertListOf.addAll(teacherData)
                    }
                    viewDelegate?.notifyAdapter()
                } else {
                    LKToastUtil.showToastShort(obj?.message)
                }
            }

            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                viewDelegate?.stopSwipRefresh()
                LKToastUtil.showToastShort(resources.getString(R.string.app_net_erro))
            }
        }
                , isShowLoder, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))
    }
    /**
     * 家长列表请求
     */
    private fun httParentList(isShowLoder:Boolean) {
        val parentClassNotifyRequestEntity = ParentClassNotifyRequestEntity()
        parentClassNotifyRequestEntity.currPage = currentPage
        LKHttp.post(CLASS_NOTIFY_PARENT, parentClassNotifyRequestEntity, ParentClassNotifyResponseEntity::class.java, object : BaseCallBack<ParentClassNotifyResponseEntity>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: ParentClassNotifyResponseEntity?) {
                super.onSuccess(url, resultText, obj)
                viewDelegate?.stopSwipRefresh()
                if (obj?.code == SKCODE_200) {
                    if (currentPage == 1) {
                        mutableParentListOf.clear()
                    }
                    val parentData = ClassNotifySever.getInstance().getParentData(obj, viewDelegate)
                    if (parentData!==null){
                        mutableParentListOf.addAll(parentData)
                    }
                    viewDelegate?.notifyAdapter()
                } else {
                    LKToastUtil.showToastShort(obj?.message)
                }
            }

            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                viewDelegate?.stopSwipRefresh()
                LKToastUtil.showToastShort(resources.getString(R.string.app_net_erro))
            }
        }
                , isShowLoder, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))
    }
}